import { ref } from "vue";
import { defineStore } from "pinia";
import type { BannerItem, CategoryItem, GuessItem, HotItem, NewItem, Page, PageParams } from "@/types/home";
import { fetchBannerAPI, fetchCategoryAPI, fetchGuessAPI, fetchHotAPI, fetchNewAPI } from "@/api/homeApi";

const useHomeStore = defineStore("homeStore", () => {
    /*轮播图数据*/
    const bannerList = ref<BannerItem[]>([]);
    /*分类的数据*/
    const categoryList = ref<CategoryItem[]>([]);
    /*热门推荐的数据*/
    const hotList = ref<HotItem[]>([]);
    /*猜你喜欢列表*/
    const guessList = ref<GuessItem[]>([]);
    /*新鲜好物列表*/
    const newList = ref<NewItem[]>([]);
    /*猜你喜欢列表总数*/
    const guessListTotal = ref<number>(0);
    /*获取轮播图数据的请求函数*/
    const fetchBanner = async (distributionSite: number): Promise<void> => {
        const { result }: { result: BannerItem[] } = await fetchBannerAPI(distributionSite);
        bannerList.value = result;
    };
    /*获取分类列表数据的请求函数*/
    const fetchCategory = async (): Promise<void> => {
        const { result }: { result: CategoryItem[] } = await fetchCategoryAPI();
        categoryList.value = result;
    };
    /*获取热门推荐数据的请求函数*/
    const fetchHot = async (): Promise<void> => {
        const { result }: { result: HotItem[] } = await fetchHotAPI();
        hotList.value = result;
    };
    /*获取猜你喜欢数据的请求函数*/
    const fetchGuess = async (page: PageParams): Promise<void> => {
        const { result }: { result: Page<GuessItem[]> } = await fetchGuessAPI(page);
        guessList.value.push(...result.items);
        guessListTotal.value = result.counts;
        page.page++;
    };
    /*获取同类推荐数据的请求函数*/
    const fetchNew = async (limit: number): Promise<void> => {
        const { result }: { result: NewItem[] } = await fetchNewAPI(limit);
        newList.value = result;
    };
    return {
        bannerList,
        categoryList,
        hotList,
        guessList,
        newList,
        guessListTotal,
        fetchBanner,
        fetchCategory,
        fetchHot,
        fetchGuess,
        fetchNew
    };

});


export default useHomeStore;
